/****** FILES FOR PROJECTED METHOD ******/

// this file just save the relevant steady-state values for projected method.

//1. save VALUE FUNCTIONS
#if NOPOLICY == 1
void saveVFforPM(double *valueUiNE, double *C_UiNE, double *saveUiNE,
                 double *valueUiE,  double *C_UiE,  double *saveUiE,
                 double *valueUnNE, double *C_UnNE, double *saveUnNE,
                 double *valueUnE,  double *C_UnE,  double *saveUnE,
                 double *valueWWNE, double *C_WWNE, double *saveWWNE,
                 double *valueWWE,  double *C_WWE,  double *saveWWE,
                 double *valueJE,   double *C_JE,   double *saveJE,
                 double *valueJNE,  double *C_JNE,  double *saveJNE)
#endif

#if SEA == 1
void saveVFforPM(double *valueUiNE, double *saveUiNE,
                 double *valueUiE, double *saveUiE,
                 double *valueUnNE, double *saveUnNE,
                 double *valueUnE, double *saveUnE,
                 double *valueWWNE, double *saveWWNE,
                 double *valueWWE, double *saveWWE,
                 double *valueJE, double *saveJE,
                 double *valueJNE, double *saveJNE,
                 double *valueJEi, double *saveJEi,
                 double *valueJNEi, double *saveJNEi)
#endif

{
    
    FILE *valuefnoutfile;
    
    int igrid, egrid, ygrid, tgrid;
    
    ///////////////////////////////////////////////////////
    // SAVE FILES FOR VALUES AT THE INITIAL STEADY STATE // (indexPMwrite == 1)
    ///////////////////////////////////////////////////////
    
#if indexPMwrite == 1
    
    /******** VALUE FUNCTIONS ********/
    
    valuefnoutfile=fopen(valueJE_0file, "w");
    setbuf ( valuefnoutfile , NULL );
    for(igrid=0;igrid<maxgrid;igrid++){
        for(int tgrid = 0; tgrid < maxtheta ; tgrid++){
            for(egrid=0;egrid<maxfirmtype;egrid++) {
                fprintf(valuefnoutfile,"%20.15f\n",valueJE[inxE(igrid, tgrid, egrid)]);
            }
        }
    }
    fclose(valuefnoutfile);
    
    
    valuefnoutfile=fopen(valueJNE_0file, "w");
    setbuf ( valuefnoutfile , NULL );
    for(igrid=0;igrid<maxgrid;igrid++){
        for(int tgrid = 0; tgrid < maxtheta ; tgrid++){
            for(egrid=0;egrid<maxfirmtype;egrid++) {
                fprintf(valuefnoutfile,"%20.15f\n",valueJNE[inxE(igrid, tgrid, egrid)]);
            }
        }
    }
    fclose(valuefnoutfile);
    
    
    valuefnoutfile=fopen(valueWWNE_0file, "w");
    setbuf ( valuefnoutfile , NULL );
    for(igrid=0;igrid<maxgrid;igrid++){
        for(int tgrid = 0; tgrid < maxtheta ; tgrid++){
            for(ygrid = 0; ygrid < maxyprod; ygrid++){
                fprintf(valuefnoutfile,"%20.15f\n",valueWWNE[inxW(igrid, tgrid, ygrid)]);
            }
        }
    }
    fclose(valuefnoutfile);
    
    valuefnoutfile=fopen(valueWWE_0file, "w");
    setbuf ( valuefnoutfile , NULL );
    for(igrid=0;igrid<maxgrid;igrid++){
        for(int tgrid = 0; tgrid < maxtheta ; tgrid++){
            for(ygrid = 0; ygrid < maxyprod; ygrid++){
                fprintf(valuefnoutfile,"%20.15f\n",valueWWE[inxW(igrid, tgrid, ygrid)]);
            }
        }
    }
    fclose(valuefnoutfile);
    
    valuefnoutfile=fopen(valueUiNE_0file, "w");
    setbuf ( valuefnoutfile , NULL );
    for(igrid=0;igrid<maxgrid;igrid++){
        for(int tgrid = 0; tgrid < maxtheta ; tgrid++){
            fprintf(valuefnoutfile,"%20.15f\n",valueUiNE[inx(igrid, tgrid)]);
        }
    }
    fclose(valuefnoutfile);
    
    valuefnoutfile=fopen(valueUnNE_0file, "w");
    setbuf ( valuefnoutfile , NULL );
    for(igrid=0;igrid<maxgrid;igrid++){
        for(int tgrid = 0; tgrid < maxtheta ; tgrid++){
            fprintf(valuefnoutfile,"%20.15f\n",valueUnNE[inx(igrid, tgrid)]);
            
        }
    }
    fclose(valuefnoutfile);
    
    valuefnoutfile=fopen(valueUiE_0file, "w");
    setbuf ( valuefnoutfile , NULL );
    for(igrid=0;igrid<maxgrid;igrid++)
    {
        for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
        {
            
            fprintf(valuefnoutfile,"%20.15f\n",valueUiE[inx(igrid, tgrid)]);
            
        }
    }
    fclose(valuefnoutfile);
    
    valuefnoutfile=fopen(valueUnE_0file, "w");
    setbuf ( valuefnoutfile , NULL );
    for(igrid=0;igrid<maxgrid;igrid++)
    {
        for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
        {
            
            fprintf(valuefnoutfile,"%20.15f\n",valueUnE[inx(igrid, tgrid)]);
            
        }
    }
    fclose(valuefnoutfile);
    
    
#if OPT_compute_C_flow == 1
    valuefnoutfile=fopen(C_JE_0file, "w");
    setbuf ( valuefnoutfile , NULL );
    for(igrid=0;igrid<maxgrid;igrid++){
        for(int tgrid = 0; tgrid < maxtheta ; tgrid++){
            for(egrid=0;egrid<maxfirmtype;egrid++) {
                fprintf(valuefnoutfile,"%20.15f\n",C_JE[inxE(igrid, tgrid, egrid)]);
            }
        }
    }
    fclose(valuefnoutfile);
    
    
    valuefnoutfile=fopen(C_JNE_0file, "w");
    setbuf ( valuefnoutfile , NULL );
    for(igrid=0;igrid<maxgrid;igrid++){
        for(int tgrid = 0; tgrid < maxtheta ; tgrid++){
            for(egrid=0;egrid<maxfirmtype;egrid++) {
                fprintf(valuefnoutfile,"%20.15f\n",C_JNE[inxE(igrid, tgrid, egrid)]);
            }
        }
    }
    fclose(valuefnoutfile);
    
    
    valuefnoutfile=fopen(C_WWNE_0file, "w");
    setbuf ( valuefnoutfile , NULL );
    for(igrid=0;igrid<maxgrid;igrid++){
        for(int tgrid = 0; tgrid < maxtheta ; tgrid++){
            for(ygrid = 0; ygrid < maxyprod; ygrid++){
                fprintf(valuefnoutfile,"%20.15f\n",C_WWNE[inxW(igrid, tgrid, ygrid)]);
            }
        }
    }
    fclose(valuefnoutfile);
    
    valuefnoutfile=fopen(C_WWE_0file, "w");
    setbuf ( valuefnoutfile , NULL );
    for(igrid=0;igrid<maxgrid;igrid++){
        for(int tgrid = 0; tgrid < maxtheta ; tgrid++){
            for(ygrid = 0; ygrid < maxyprod; ygrid++){
                fprintf(valuefnoutfile,"%20.15f\n",C_WWE[inxW(igrid, tgrid, ygrid)]);
            }
        }
    }
    fclose(valuefnoutfile);
    
    valuefnoutfile=fopen(C_UiNE_0file, "w");
    setbuf ( valuefnoutfile , NULL );
    for(igrid=0;igrid<maxgrid;igrid++){
        for(int tgrid = 0; tgrid < maxtheta ; tgrid++){
            fprintf(valuefnoutfile,"%20.15f\n",C_UiNE[inx(igrid, tgrid)]);
        }
    }
    fclose(valuefnoutfile);
    
    valuefnoutfile=fopen(C_UnNE_0file, "w");
    setbuf ( valuefnoutfile , NULL );
    for(igrid=0;igrid<maxgrid;igrid++){
        for(int tgrid = 0; tgrid < maxtheta ; tgrid++){
            fprintf(valuefnoutfile,"%20.15f\n",C_UnNE[inx(igrid, tgrid)]);
            
        }
    }
    fclose(valuefnoutfile);
    
    valuefnoutfile=fopen(C_UiE_0file, "w");
    setbuf ( valuefnoutfile , NULL );
    for(igrid=0;igrid<maxgrid;igrid++)
    {
        for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
        {
            
            fprintf(valuefnoutfile,"%20.15f\n",C_UiE[inx(igrid, tgrid)]);
            
        }
    }
    fclose(valuefnoutfile);
    
    valuefnoutfile=fopen(C_UnE_0file, "w");
    setbuf ( valuefnoutfile , NULL );
    for(igrid=0;igrid<maxgrid;igrid++)
    {
        for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
        {
            
            fprintf(valuefnoutfile,"%20.15f\n",C_UnE[inx(igrid, tgrid)]);
            
        }
    }
    fclose(valuefnoutfile);
#endif
#endif
    
    
    
    ///////////////////////////////////////////////////
    // SAVE FILES FOR VALUES AT THE NEW STEADY STATE // (indexPMwrite == 2)
    ///////////////////////////////////////////////////
    
    
#if indexPMwrite == 2
    
    /******** VALUE FUNCTIONS *******/
    
    valuefnoutfile=fopen(valueJE_Tfile, "w");
    setbuf ( valuefnoutfile , NULL );
    for(igrid=0;igrid<maxgrid;igrid++)
    {
        for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
        {
            for(egrid=0;egrid<maxfirmtype;egrid++) {
                fprintf(valuefnoutfile,"%20.15f\n",valueJE[inxE(igrid, tgrid, egrid)]);
            }
        }
    }
    fclose(valuefnoutfile);
    
    valuefnoutfile=fopen(valueJNE_Tfile, "w");
    setbuf ( valuefnoutfile , NULL );
    for(igrid=0;igrid<maxgrid;igrid++)
    {
        for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
        {
            for(egrid=0;egrid<maxfirmtype;egrid++) {
                fprintf(valuefnoutfile,"%20.15f\n",valueJNE[inxE(igrid, tgrid, egrid)]);
            }
        }
    }
    fclose(valuefnoutfile);
    
    
    
    // IF SEA == 1, then you need to write another value function.
#if SEA == 1
    valuefnoutfile=fopen(valueJEi_Tfile, "w");
    setbuf ( valuefnoutfile , NULL );
    for(igrid=0;igrid<maxgrid;igrid++)
    {
        for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
        {
            for(egrid=0;egrid<maxfirmtype;egrid++) {
                fprintf(valuefnoutfile,"%20.15f\n",valueJEi[inxE(igrid, tgrid, egrid)]);
            }
        }
    }
    fclose(valuefnoutfile);
    
    valuefnoutfile=fopen(valueJNEi_Tfile, "w");
    setbuf ( valuefnoutfile , NULL );
    for(igrid=0;igrid<maxgrid;igrid++)
    {
        for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
        {
            for(egrid=0;egrid<maxfirmtype;egrid++) {
                fprintf(valuefnoutfile,"%20.15f\n",valueJNEi[inxE(igrid, tgrid, egrid)]);
            }
        }
    }
    fclose(valuefnoutfile);
#endif
    
    
    valuefnoutfile=fopen(valueWWNE_Tfile, "w");
    setbuf ( valuefnoutfile , NULL );
    for(igrid=0;igrid<maxgrid;igrid++)
    {
        for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
        {
            for(ygrid = 0; ygrid < maxyprod; ygrid++){
                fprintf(valuefnoutfile,"%20.15f\n",valueWWNE[inxW(igrid, tgrid, ygrid)]);
            }
        }
    }
    fclose(valuefnoutfile);
    
    valuefnoutfile=fopen(valueWWE_Tfile, "w");
    setbuf ( valuefnoutfile , NULL );
    for(igrid=0;igrid<maxgrid;igrid++)
    {
        for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
        {
            for(ygrid = 0; ygrid < maxyprod; ygrid++){
                fprintf(valuefnoutfile,"%20.15f\n",valueWWE[inxW(igrid, tgrid, ygrid)]);
            }
        }
    }
    fclose(valuefnoutfile);
    
    valuefnoutfile=fopen(valueUiNE_Tfile, "w");
    setbuf ( valuefnoutfile , NULL );
    for(igrid=0;igrid<maxgrid;igrid++)
    {
        for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
        {
            fprintf(valuefnoutfile,"%20.15f\n",valueUiNE[inx(igrid, tgrid)]);
        }
    }
    fclose(valuefnoutfile);
    
    valuefnoutfile=fopen(valueUnNE_Tfile, "w");
    setbuf ( valuefnoutfile , NULL );
    for(igrid=0;igrid<maxgrid;igrid++)
    {
        for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
        {
            fprintf(valuefnoutfile,"%20.15f\n", valueUnNE[inx(igrid, tgrid)]);
        }
    }
    fclose(valuefnoutfile);
    
    valuefnoutfile=fopen(valueUiE_Tfile, "w");
    setbuf ( valuefnoutfile , NULL );
    for(igrid=0;igrid<maxgrid;igrid++)
    {
        for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
        {
            fprintf(valuefnoutfile,"%20.15f\n",valueUiE[inx(igrid, tgrid)]);
        }
    }
    fclose(valuefnoutfile);
    
    valuefnoutfile=fopen(valueUnE_Tfile, "w");
    setbuf ( valuefnoutfile , NULL );
    for(igrid=0;igrid<maxgrid;igrid++)
    {
        for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
        {
            fprintf(valuefnoutfile,"%20.15f\n",valueUnE[inx(igrid, tgrid)]);
        }
    }
    fclose(valuefnoutfile);
    
    
    
    /******* SAVINGS *******/
    valuefnoutfile=fopen(saveJE_Tfile, "w");
    setbuf ( valuefnoutfile , NULL );
    for(igrid=0;igrid<maxgrid;igrid++)
    {
        for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
        {
            for(egrid=0;egrid<maxfirmtype;egrid++) {
                fprintf(valuefnoutfile,"%20.15f\n",saveJE[inxE(igrid, tgrid, egrid)]);
            }
        }
    }
    fclose(valuefnoutfile);
    
    valuefnoutfile=fopen(saveJNE_Tfile, "w");
    setbuf ( valuefnoutfile , NULL );
    for(igrid=0;igrid<maxgrid;igrid++)
    {
        for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
        {
            for(egrid=0;egrid<maxfirmtype;egrid++) {
                fprintf(valuefnoutfile,"%20.15f\n",saveJNE[inxE(igrid, tgrid, egrid)]);
            }
        }
    }
    fclose(valuefnoutfile);
    
#if SEA == 1
    valuefnoutfile=fopen(saveJEi_Tfile, "w");
    setbuf ( valuefnoutfile , NULL );
    for(igrid=0;igrid<maxgrid;igrid++)
    {
        for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
        {
            for(egrid=0;egrid<maxfirmtype;egrid++) {
                fprintf(valuefnoutfile,"%20.15f\n",saveJEi[inxE(igrid, tgrid, egrid)]);
            }
        }
    }
    fclose(valuefnoutfile);
    
    valuefnoutfile=fopen(saveJNEi_Tfile, "w");
    setbuf ( valuefnoutfile , NULL );
    for(igrid=0;igrid<maxgrid;igrid++)
    {
        for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
        {
            for(egrid=0;egrid<maxfirmtype;egrid++) {
                fprintf(valuefnoutfile,"%20.15f\n",saveJNEi[inxE(igrid, tgrid, egrid)]);
            }
        }
    }
    fclose(valuefnoutfile);
#endif
    
    valuefnoutfile=fopen(saveWWNE_Tfile, "w");
    setbuf ( valuefnoutfile , NULL );
    for(igrid=0;igrid<maxgrid;igrid++)
    {
        for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
        {
            for(ygrid = 0; ygrid < maxyprod; ygrid++){
                fprintf(valuefnoutfile,"%20.15f\n",saveWWNE[inxW(igrid, tgrid, ygrid)]);
            }
        }
    }
    fclose(valuefnoutfile);
    
    valuefnoutfile=fopen(saveWWE_Tfile, "w");
    setbuf ( valuefnoutfile , NULL );
    for(igrid=0;igrid<maxgrid;igrid++)
    {
        for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
        {
            for(ygrid = 0; ygrid < maxyprod; ygrid++){
                fprintf(valuefnoutfile,"%20.15f\n",saveWWE[inxW(igrid, tgrid, ygrid)]);
            }
        }
    }
    fclose(valuefnoutfile);
    
    valuefnoutfile=fopen(saveUiNE_Tfile, "w");
    setbuf ( valuefnoutfile , NULL );
    for(igrid=0;igrid<maxgrid;igrid++)
    {
        for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
        {
            fprintf(valuefnoutfile,"%20.15f\n",saveUiNE[inx(igrid, tgrid)]);
        }
    }
    fclose(valuefnoutfile);
    
    valuefnoutfile=fopen(saveUnNE_Tfile, "w");
    setbuf ( valuefnoutfile , NULL );
    for(igrid=0;igrid<maxgrid;igrid++)
    {
        for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
        {
            fprintf(valuefnoutfile,"%20.15f\n",saveUnNE[inx(igrid, tgrid)]);
        }
    }
    fclose(valuefnoutfile);
    
    valuefnoutfile=fopen(saveUiE_Tfile, "w");
    setbuf ( valuefnoutfile , NULL );
    for(igrid=0;igrid<maxgrid;igrid++)
    {
        for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
        {
            fprintf(valuefnoutfile,"%20.15f\n",saveUiE[inx(igrid, tgrid)]);
        }
    }
    fclose(valuefnoutfile);
    
    valuefnoutfile=fopen(saveUnE_Tfile, "w");
    setbuf ( valuefnoutfile , NULL );
    for(igrid=0;igrid<maxgrid;igrid++)
    {
        for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
        {
            fprintf(valuefnoutfile,"%20.15f\n",saveUnE[inx(igrid, tgrid)]);
        }
    }
    fclose(valuefnoutfile);
    
#endif
    
}



#if SEA == 0
void saveDISTforPM(double *distJE, double *distJNE, double *distWWNE, double *distWWE, double *distUnE, double *distUnNE, double *distUiNE, double *distUiE)
{
#endif
#if SEA == 1
    void saveDISTforPM(double *distJE, double *distJNE, double *distJEi, double *distJNEi, double *distWWNE, double *distWWE, double *distUnE, double *distUnNE, double *distUiNE, double *distUiE)
    {
#endif
        
        FILE *distoutfile;
        
        int igrid, egrid, ygrid, tgrid;
        
        ///////////////////////////////////////////////////////
        // SAVE FILES FOR VALUES AT THE INITIAL STEADY STATE // (indexPMwrite == 1)
        ///////////////////////////////////////////////////////
        
        
#if indexPMwrite == 1
        
        /******** DISTRIBUTIONS INITIALES ********/
        
        distoutfile=fopen(distJE_0file, "w");
        setbuf ( distoutfile , NULL );
        for(igrid=0;igrid<maxgrid;igrid++)
        {
            for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
            {
                for(egrid=0;egrid<maxfirmtype;egrid++) {
                    fprintf(distoutfile,"%20.15f\n",distJE[inxE(igrid, tgrid, egrid)]);
                }
            }
        }
        fclose(distoutfile);
        
        distoutfile=fopen(distJNE_0file, "w");
        setbuf ( distoutfile , NULL );
        for(igrid=0;igrid<maxgrid;igrid++)
        {
            for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
            {
                for(egrid=0;egrid<maxfirmtype;egrid++) {
                    fprintf(distoutfile,"%20.15f\n",distJNE[inxE(igrid, tgrid, egrid)]);
                }
            }
        }
        fclose(distoutfile);
        
        distoutfile=fopen(distWWNE_0file, "w");
        setbuf ( distoutfile , NULL );
        for(igrid=0;igrid<maxgrid;igrid++)
        {
            for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
            {
                for(ygrid = 0; ygrid < maxyprod; ygrid++){
                    fprintf(distoutfile,"%20.15f\n",distWWNE[inxW(igrid, tgrid, ygrid)]);
                }
            }
        }
        fclose(distoutfile);
        
        distoutfile=fopen(distWWE_0file, "w");
        setbuf ( distoutfile , NULL );
        for(igrid=0;igrid<maxgrid;igrid++)
        {
            for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
            {
                for(ygrid = 0; ygrid < maxyprod; ygrid++){
                    fprintf(distoutfile,"%20.15f\n",distWWE[inxW(igrid, tgrid, ygrid)]);
                }
            }
        }
        fclose(distoutfile);
        
        distoutfile=fopen(distUiNE_0file, "w");
        setbuf ( distoutfile , NULL );
        for(igrid=0;igrid<maxgrid;igrid++)
        {
            for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
            {
                fprintf(distoutfile,"%20.15f\n",distUiNE[inx(igrid, tgrid)]);
            }
        }
        fclose(distoutfile);
        
        distoutfile=fopen(distUnNE_0file, "w");
        setbuf ( distoutfile , NULL );
        for(igrid=0;igrid<maxgrid;igrid++)
        {
            for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
            {
                fprintf(distoutfile,"%20.15f\n",distUnNE[inx(igrid, tgrid)]);
            }
        }
        fclose(distoutfile);
        
        distoutfile=fopen(distUiE_0file, "w");
        setbuf ( distoutfile , NULL );
        for(igrid=0;igrid<maxgrid;igrid++)
        {
            for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
            {
                fprintf(distoutfile,"%20.15f\n",distUiE[inx(igrid, tgrid)]);
            }
        }
        fclose(distoutfile);
        
        distoutfile=fopen(distUnE_0file, "w");
        setbuf ( distoutfile , NULL );
        for(igrid=0;igrid<maxgrid;igrid++)
        {
            for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
            {
                fprintf(distoutfile,"%20.15f\n",distUnE[inx(igrid, tgrid)]);
            }
        }
        fclose(distoutfile);
        
#endif
        
        
        
        ///////////////////////////////////////////////////
        // SAVE FILES FOR VALUES AT THE NEW STEADY STATE // (indexPMwrite == 2)
        ///////////////////////////////////////////////////
        
#if indexPMwrite == 2
        
        /******** DISTRIBUTIONS NOUVELLES ********/
        
        distoutfile=fopen(distJE_Tfile, "w");
        setbuf ( distoutfile , NULL );
        for(igrid=0;igrid<maxgrid;igrid++)
        {
            for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
            {
                for(egrid=0;egrid<maxfirmtype;egrid++) {
                    fprintf(distoutfile,"%20.15f\n",distJE[inxE(igrid, tgrid, egrid)]);
                }
            }
        }
        fclose(distoutfile);
        
        distoutfile=fopen(distJNE_Tfile, "w");
        setbuf ( distoutfile , NULL );
        for(igrid=0;igrid<maxgrid;igrid++)
        {
            for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
            {
                for(egrid=0;egrid<maxfirmtype;egrid++) {
                    fprintf(distoutfile,"%20.15f\n",distJNE[inxE(igrid, tgrid, egrid)]);
                }
            }
        }
        fclose(distoutfile);
        
#if SEA == 1
        distoutfile=fopen(distJEi_Tfile, "w");
        setbuf ( distoutfile , NULL );
        for(igrid=0;igrid<maxgrid;igrid++)
        {
            for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
            {
                for(egrid=0;egrid<maxfirmtype;egrid++) {
                    fprintf(distoutfile,"%20.15f\n",distJEi[inxE(igrid, tgrid, egrid)]);
                }
            }
        }
        fclose(distoutfile);
        
        distoutfile=fopen(distJNEi_Tfile, "w");
        setbuf ( distoutfile , NULL );
        for(igrid=0;igrid<maxgrid;igrid++)
        {
            for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
            {
                for(egrid=0;egrid<maxfirmtype;egrid++) {
                    fprintf(distoutfile,"%20.15f\n",distJNEi[inxE(igrid, tgrid, egrid)]);
                }
            }
        }
        fclose(distoutfile);
#endif
        
        distoutfile=fopen(distWWNE_Tfile, "w");
        setbuf ( distoutfile , NULL );
        for(igrid=0;igrid<maxgrid;igrid++)
        {
            for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
            {
                for(ygrid = 0; ygrid < maxyprod; ygrid++){
                    fprintf(distoutfile,"%20.15f\n",distWWNE[inxW(igrid, tgrid, ygrid)]);
                }
            }
        }
        fclose(distoutfile);
        
        distoutfile=fopen(distWWE_Tfile, "w");
        setbuf ( distoutfile , NULL );
        for(igrid=0;igrid<maxgrid;igrid++)
        {
            for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
            {
                for(ygrid = 0; ygrid < maxyprod; ygrid++){
                    fprintf(distoutfile,"%20.15f\n",distWWE[inxW(igrid, tgrid, ygrid)]);
                }
            }
        }
        fclose(distoutfile);
        
        distoutfile=fopen(distUiNE_Tfile, "w");
        setbuf ( distoutfile , NULL );
        for(igrid=0;igrid<maxgrid;igrid++)
        {
            for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
            {
                fprintf(distoutfile,"%20.15f\n",distUiNE[inx(igrid, tgrid)]);
            }
        }
        fclose(distoutfile);
        
        distoutfile=fopen(distUnNE_Tfile, "w");
        setbuf ( distoutfile , NULL );
        for(igrid=0;igrid<maxgrid;igrid++)
        {
            for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
            {
                fprintf(distoutfile,"%20.15f\n",distUnNE[inx(igrid, tgrid)]);
            }
        }
        fclose(distoutfile);
        
        distoutfile=fopen(distUiE_Tfile, "w");
        setbuf ( distoutfile , NULL );
        for(igrid=0;igrid<maxgrid;igrid++)
        {
            for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
            {
                fprintf(distoutfile,"%20.15f\n",distUiE[inx(igrid, tgrid)]);
            }
        }
        fclose(distoutfile);
        
        distoutfile=fopen(distUnE_Tfile, "w");
        setbuf ( distoutfile , NULL );
        for(igrid=0;igrid<maxgrid;igrid++)
        {
            for(int tgrid = 0; tgrid < maxtheta ; tgrid++)
            {
                fprintf(distoutfile,"%20.15f\n",distUnE[inx(igrid, tgrid)]);
            }
        }
        fclose(distoutfile);
        
#endif  // end indexPMwrite2
        
        
    }
